package com.aide.simplification.codelib.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.ConfigurationException;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;

import com.aide.simplification.global.GlobalConfig;
import com.aide.simplification.global.Utils;
import com.aide.simplification.global.db.DatabaseOP;

@RemoteProxy(name = "Codelib")
public class Codelib {
	protected DatabaseOP dbop;

	public Codelib() {
		dbop = new DatabaseOP(Utils.getConfig("codelib.db.pool"));
	}

	public CodelibBean getDataMate(String codeType) {
		String key = "codes." + codeType + ".";
		String isConfig = Utils.getConfig("codelib.isConfig");
		CodelibBean cb = new CodelibBean();
		GlobalConfig gc = null;
		try {
			gc = new GlobalConfig(Utils.getConfig("codelib.configfile"));
		} catch (ConfigurationException e) {
		}
		if ("true".equals(isConfig)) {
			String value = gc.getString(key + "indexid");
			if (value != null && !value.equals("")) {
				cb.setIndexid(value);
			} else {
				cb.setIndexid(codeType + "id");
			}
		} else {
			cb.setIndexid(codeType + "id");
		}
		if ("true".equals(isConfig)) {
			String value = gc.getString(key + "indexname");
			if (value != null && !value.equals("")) {
				cb.setIndexname(value);
			} else {
				cb.setIndexname(codeType + "name");
			}
		} else {
			cb.setIndexname(codeType + "name");
		}
		if ("true".equals(isConfig)) {
			String value = gc.getString(key + "tablename");
			if (value != null && !value.equals("")) {
				cb.setTablename(value);
			} else {
				cb.setTablename(codeType + "code");
			}
		} else {
			cb.setTablename(codeType + "code");
		}
		if ("true".equals(isConfig)) {
			String value = gc.getString(key + "sign");
			if (value != null && !value.equals("")) {
				cb.setSign(value);
			} else {
				cb.setSign(codeType + "sign");
			}
		} else {
			cb.setSign(codeType + "sign");
		}
		return cb;
	}

	@RemoteMethod
	public Map<String, Object> findById(String codeType, String value) {
		CodelibBean cb = this.getDataMate(codeType);
		String sql = "select " + cb.getIndexid() + " as id,"
				+ cb.getIndexname() + " as name," + cb.getSign()
				+ " as sign from " + cb.getTablename() + " where "
				+ cb.getIndexid() + "=?";
		try {
			return dbop.queryForMap(sql, new Object[] { value });
		} catch (Exception e) {
			return null;
		}
	}

	@RemoteMethod
	public List<Map<String, Object>> findByLikeId(String codeType, String value) {
		CodelibBean cb = this.getDataMate(codeType);
		String sql = "select " + cb.getIndexid() + " as id," + codeType
				+ "name as name," + cb.getSign() + " as sign from " + codeType
				+ "code where " + cb.getIndexid() + " like ?";

		try {
			return dbop.queryForList(sql, new Object[] { value + "%" });
		} catch (Exception e) {
			return null;
		}
	}

	@RemoteMethod
	public List<Map<String, Object>> findByLikeIdPage(String codeType,
			String value, int page, int pagesize) {
		CodelibBean cb = this.getDataMate(codeType);
		String sql = "select " + cb.getIndexid() + " as id," + codeType
				+ "name as name," + cb.getSign() + " as sign from " + codeType
				+ "code where " + cb.getIndexid() + " like ?";
		try {
			return dbop.queryForListPage(sql, new Object[] { value + "%" },
					"id", page, pagesize);
		} catch (Exception e) {
			return null;
		}
	}

	public List<Map<String, Object>> find(String sql, Object[] params) {
		return dbop.queryForList(sql, params);
	}

	public List<Map<String, Object>> find(String sql) {
		return dbop.queryForList(sql);
	}

	@RemoteMethod
	public List<Map<String, Object>> findByLikeName(String codeType,
			String value) {
		CodelibBean cb = this.getDataMate(codeType);
		String sql = "select " + cb.getIndexid() + " as id," + codeType
				+ "name as name," + cb.getSign() + " as sign from " + codeType
				+ "code where " + cb.getIndexname() + " like ?";

		try {
			return dbop.queryForList(sql, new Object[] { value + "%" });
		} catch (Exception e) {
			return null;
		}
	}

	@RemoteMethod
	public List<Map<String, Object>> findByLikeNamePage(String codeType,
			String value, int page, int pagesize) {
		CodelibBean cb = this.getDataMate(codeType);
		String sql = "select " + cb.getIndexid() + " as id," + codeType
				+ "name as name," + cb.getSign() + " as sign from " + codeType
				+ "code where " + cb.getIndexname() + " like ?";
		try {
			return dbop.queryForListPage(sql, new Object[] { value + "%" },
					"id", page, pagesize);
		} catch (Exception e) {
			return null;
		}
	}

	@RemoteMethod
	public List<Map<String, Object>> findAllPage(String codeType, int page,
			int pagesize) {
		CodelibBean cb = this.getDataMate(codeType);
		String sql = "select " + cb.getIndexid() + " as id," + codeType
				+ "name as name," + cb.getSign() + " as sign from " + codeType
				+ "code";
		try {
			return dbop.queryForListPage(sql, "id", page, pagesize);
		} catch (Exception e) {
			return null;
		}
	}

	@RemoteMethod
	public int save(String codeType, String id, String name, int sign) {
		CodelibBean cb = this.getDataMate(codeType);
		Map<String, Object> hm = new HashMap<String, Object>();
		hm.put(cb.getIndexid() + "", id);
		hm.put(cb.getIndexname() + "", name);
		hm.put(cb.getSign() + "", sign);
		return dbop.save(cb.getTablename() + "", hm);
	}

	@RemoteMethod
	public int update(String codeType, String id, String name, int sign) {
		CodelibBean cb = this.getDataMate(codeType);
		Map<String, Object> hm = new HashMap<String, Object>();
		hm.put(cb.getIndexid() + "", id);
		hm.put(cb.getIndexname() + "", name);
		hm.put(cb.getSign() + "", sign);
		return dbop
				.update(cb.getTablename() + "", hm, cb.getIndexid() + "", id);
	}

	@RemoteMethod
	public int deleteCode(String codeType, String id) {
		CodelibBean cb = this.getDataMate(codeType);
		return dbop.delete(cb.getTablename() + "", cb.getIndexid() + "", id);
	}

	public List<Map<String, Object>> getCodeLibList() {

		return null;
	}

	@RemoteMethod
	public List<Map<String, Object>> chakan(String codeType, String value) {
		Map<String, Object> hm = this.findById(codeType, value);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (hm != null) {
			Map<String, Object> hm0 = new HashMap<String, Object>();
			hm0.put("title", "代码编号:");
			hm0.put("tname", "id");
			hm0.put("tvalue", hm.get("id").toString());
			list.add(hm0);

			Map<String, Object> hm1 = new HashMap<String, Object>();
			hm1.put("title", "代码名称:");
			hm1.put("tname", "name");
			hm1.put("tvalue", hm.get("name").toString());
			list.add(hm1);

			Map<String, Object> hm4 = new HashMap<String, Object>();
			hm4.put("title", "有效标记:");
			hm4.put("tname", "sign");
			hm4.put("tval", hm.get("sign"));
			if (hm.get("sign").toString().equals("1")) {
				hm4.put("tvalue", "可用");
			} else {
				hm4.put("tvalue", "不可用");
			}
			list.add(hm4);
		}
		return list;
	}

	@RemoteMethod
	public int checkPName(String codeType, String codeid, String codename,
			String type) {
		CodelibBean cb = this.getDataMate(codeType);
		if (codename.indexOf("'") != -1 || codename.length() <= 0) {
			return 0;
		}
		String sql = "select count(*) as id from " + cb.getTablename()
				+ " where (" + cb.getIndexname() + "=? or " + cb.getIndexid()
				+ "=?)";

		if (type.equals("update")) {
			sql += " and " + cb.getIndexid() + "!=?";
			if (dbop
					.queryForInt(sql, new Object[] { codename, codeid, codeid }) == 0) {
				return 1;
			}
		} else if (dbop.queryForInt(sql, new Object[] { codename, codeid }) == 0) {
			return 1;
		}
		return 0;
	}

	@RemoteMethod
	public int getCount(String codeType) {
		CodelibBean cb = this.getDataMate(codeType);
		return dbop.queryForInt("select count(*) from " + cb.getTablename()
				+ "");
	}

	@RemoteMethod
	public int getCountById(String codeType, String id) {
		CodelibBean cb = this.getDataMate(codeType);
		return dbop.queryForInt("select count(*) from " + codeType
				+ "code where " + cb.getIndexid() + " like ?",
				new Object[] { id + "%" });
	}

	@RemoteMethod
	public int getCountByName(String codeType, String name) {
		CodelibBean cb = this.getDataMate(codeType);
		return dbop.queryForInt("select count(*) from " + codeType
				+ "code where " + cb.getIndexname() + " like?",
				new Object[] { name + "%" });
	}

	@RemoteMethod
	public List<Map<String, String>> getCodeTypeList() {

		try {
			GlobalConfig gc = new GlobalConfig(Utils
					.getConfig("codelib.configfile"));
			Iterator<String> it = gc.getRegex("\\.\\w+\\.", "codes");
			List<Map<String, String>> list = new ArrayList<Map<String, String>>();
			while (it.hasNext()) {
				String key = it.next();
				Map<String, String> map = new HashMap<String, String>();
				map.put("name", gc.getString("codes" + key + "name"));
				map.put("id", Utils.find(key, "\\w+"));
				list.add(map);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
